package com.bwie.service.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bwie.utils.R;
import com.bwie.utils.TokenUtil;
import com.bwie.vo.PageInfoVo;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.Type;
import com.bwie.mapper.TypeMapper;
import com.bwie.service.TypeService;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: 鑫仔
 * @Date: 2025/7/21 14:20
 * @Description:
 **/

@Service
public class TypeServiceImpl extends ServiceImpl<TypeMapper, Type> implements TypeService {

    @Autowired
    private TypeMapper typeMapper;

    @Autowired
    private FastFileStorageClient fileStorageClient;

    @Autowired
    private HttpServletRequest request;


    @Override
    public R getTypeTree(PageInfoVo pageInfoVo) {
//        //查询
//        LambdaQueryWrapper<Type> wrapper = new LambdaQueryWrapper<Type>();
//        //名称模糊查询
//        if (ObjectUtils.isNotEmpty(pageInfoVo.getKeyWord()) && pageInfoVo != null) {
//            wrapper.like(Type::getTypeName, pageInfoVo.getKeyWord());
//        }
//
//
//        if (ObjectUtils.isNotEmpty(pageInfoVo.getTypeId()) && pageInfoVo != null) {
//            wrapper.eq(Type::getTypeStatus, pageInfoVo.getTypeId());
//        }


        List<Type> typeList = typeMapper.getTypeTree(pageInfoVo);

        //树形结构
        List<Tree<String>> treeList = TreeUtil.build(typeList, "0", (type, treeNode) -> {
            treeNode.setId(type.getTypeId().toString());
            treeNode.setParentId(type.getTypePid().toString());
            treeNode.setName(type.getTypeName());

            treeNode.putExtra("typeImage", type.getTypeImage());
            treeNode.put("typeTop", type.getTypeTop());
            treeNode.putExtra("typeStatus", type.getTypeStatus());

        });
        return R.OK(treeList);
    }

    @Override
    public R uploadFile(MultipartFile file) {
        try {
            String extName = FileUtil.extName(file.getOriginalFilename());
            StorePath storePath = fileStorageClient.uploadFile(file.getInputStream(), file.getSize(), extName, null);

            String path = "http://192.168.80.192:8888/" + storePath.getFullPath();
            return R.OK(path);
        } catch (IOException e) {

            return R.ERROR(500, "上传失败");
        }
    }

    @Override
    public R addType(Type type) {
        //获取登录人名称
        String token = request.getHeader("token");
        String userName = TokenUtil.getUserName(token);

        //分类名称是否唯一
        Type typeDb = typeMapper.selectOne(new LambdaQueryWrapper<Type>().eq(Type::getTypeName, type.getTypeName()));
        if (ObjectUtils.isNotEmpty(typeDb)) {
            return R.ERROR(500, "不可重复添加分类");
        }

        //设置审计字段
        type.setCreateBy(userName);
        type.setCreateTime(new Date());

        //添加
        typeMapper.insert(type);
        return R.OK();
    }

    @Override
    public R getSelectType() {
        List<Type> typeList = typeMapper.selectList(null);

        List<Tree<String>> treeList = TreeUtil.build(typeList, "0", (type, treeNode) -> {
            treeNode.setName(type.getTypeName());
            treeNode.setId(type.getTypeId().toString());
            treeNode.setParentId(type.getTypePid().toString());

            treeNode.putExtra("value", type.getTypeId());
            treeNode.putExtra("label", type.getTypeName());
        });
        return R.OK(treeList);
    }
}
